home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / kertrans.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  10KB  |  306 lines

  1. /* kertrans.c transfer and send functions to support file sends through
  2.  * ueMail.  All functions available only through kermit().
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <osbind.h>
  7. #include "ed.h"
  8. #include "kermit.h"
  9.  
  10. /* ASCII transmit.  Some delay ability.  Use for short files only or over
  11.  * a direct link.
  12.  */
  13. extern int emacsfile;
  14. extern char getfiln[NFILEN];
  15.  
  16. int
  17. trans()
  18. {
  19.         register int c;
  20.         register int ln = 0;
  21.  
  22.         while (curwp->w_dotp != curbp->b_linep)
  23.                 {
  24.                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  25.                 if (curwp->w_doto == llength(curwp->w_dotp))
  26.                         {
  27.                         mlwrite("[Transmitting line: %d]",++ln);
  28.                         if (!emacsfile)
  29.                                 c = '\r';
  30.                         else
  31.                                 c = '\n';
  32.                         }
  33.                 while (!(int)Bcostat(1))        /* check for line ready */
  34.                         {
  35.                         if (Bconstat(2))
  36.                                 if (ttgetc()==0x07)
  37.                                         return(FALSE);
  38.                         }
  39.                 sendaux(c);
  40.                 if (!forwchar(FALSE, 1))
  41.                         return(FALSE);
  42.                 }
  43.         if(!emacsfile)
  44.                 {
  45.                 while (!(int)Bcostat(1))
  46.                         {
  47.                         if (Bconstat(2))
  48.                                 if (ttgetc()==0x07)
  49.                                         return(FALSE);
  50.                         }
  51.                 sendaux(CLEAR);
  52.                 }
  53.         return(ln);
  54. }
  55.  
  56. /*
  57. *      s e n d s w
  58. *
  59. * Sendsw is the state table switcher for sending files. It loops until
  60. * either it finishes, or an error is encountered. The routines called
  61. * by sendsw are responsible for changing the state.
  62. *
  63. */
  64. int
  65. sendsw()
  66. {
  67.  
  68.         state = 'S';        /* Send initiate is the start state */
  69.         n = np = 0;               /* Initialize message number */
  70.         numtry = 0;          /* Say no tries yet */
  71.         while(TRUE)          /* Do this as long as necessary */
  72.         {
  73.                 switch(state)
  74.                 {
  75.                 case 'S':
  76.                         mlwrite("[Sending init packet]");
  77.                         state = sinit();
  78.                         break; /* Send-Init */
  79.                 case 'F':
  80.                         state = sfile();
  81.                         break; /* Send-File */
  82.                 case 'D':
  83.                         state = sdata();
  84.                         break; /* Send-Data */
  85.                 case 'Z':
  86.                         state = seof();
  87.                         break; /* Send-End-of-File */
  88.                 case 'B':
  89.                         state = sbreak();
  90.                         break; /* Send-Break */
  91.                 case 'C':
  92.                         return (TRUE);    /* Complete */
  93.                 case 'A':
  94.                         return (FALSE);  /* "Abort" */
  95.                 default:
  96.                         return (FALSE);  /* Unknown, fail */
  97.                 }
  98.         }
  99. }
  100.  
  101.  
  102. /*
  103. *      s i n i t
  104. *
  105. * Send Initiate: send this host's parameters and get other side's back.
  106. */
  107.  
  108. char sinit()
  109. {
  110.         int num, len;              /* Packet number, length */
  111.         /* If too many tries, give up */
  112.         if (numtry++ > MAXTRY) return('A');
  113.         len = spar(packet);                  /* Fill up init info packet */
  114.  
  115.  
  116.         spack('S',n,len,packet);                /* Send an S packet */
  117.         switch(rpack(&len,&num,recpkt)) /* What was the reply? */
  118.         {
  119.         case 'N':
  120.                 return(state);    /* NAK, try it again */
  121.  
  122.         case 'Y':                      /* ACK */
  123.                 if (n != num)      /* If wrong ACK, stay in S state */
  124.                         return(state);  /* and try again */
  125.                 rpar(recpkt,len);       /* Get other side's init info */
  126.  
  127.                 numtry = 0;          /* Reset try counter */
  128.                 n = (n+1)%64;      /* Bump packet count */
  129.                 return('F');        /* OK, switch state to F */
  130.  
  131.         case 'E':                      /* Error packet received */
  132.                 prerrpkt(recpkt);       /* Print it out and */
  133.                 return('A');        /* abort */
  134.  
  135.         case FALSE:
  136.                 return(state);    /* Receive failure, try again */
  137.  
  138.         default:
  139.                 return('A');        /* Anything else, just "abort" */
  140.         }
  141. }
  142.  
  143.  
  144. /*
  145. *      s f i l e
  146. *
  147. * Send File Header.
  148. */
  149.  
  150. char sfile()
  151. {
  152.         int num, len;              /* Packet number, length */
  153.  
  154.         /* If too many tries, give up */
  155.         if (numtry++ > MAXTRY) return('A');
  156.         len = strlen(getfiln);
  157.         mlwrite("[Sending %s as %s]",curbp->b_bname,getfiln);
  158.         spack('F',n,len,getfiln);            /* Send an F packet */
  159.         switch(rpack(&len,&num,recpkt))  /* What was the reply? */
  160.         {
  161.         case 'N':                      /* NAK, just stay in this state, */
  162.                 /* unless it's NAK for next packet */
  163.                 num = (--num<0 ? 63:num);
  164.                 if (n != num)      /* which is just like an ACK for */
  165.                         return(state);  /* this packet so fall thru to... */
  166.  
  167.         case 'Y':                      /* ACK */
  168.                 if (n != num) return(state); /* If wrong ACK, stay in F state */
  169.                 numtry = 0;          /* Reset try counter */
  170.                 n = (n+1)%64;      /* Bump packet count */
  171.                 size = bufill(packet);  /* Get first data from file */
  172.                 return('D');        /* Switch state to D */
  173.  
  174.         case 'E':                      /* Error packet received */
  175.                 prerrpkt(recpkt);       /* Print it out and */
  176.                 return('A');        /* abort */
  177.  
  178.         case FALSE:
  179.                 return(state);    /* Receive failure, stay in F state */
  180.  
  181.         default:
  182.                 return('A');        /* Something else, just "abort" */
  183.         }
  184. }
  185.  
  186.  
  187. /*
  188. *      s d a t a
  189. *
  190. * Send File Data
  191. */
  192.  
  193. char sdata()
  194. {
  195.         int num, len;              /* Packet number, length */
  196.  
  197.         if (numtry++ > MAXTRY) return('A'); /* If too many tries, give up */
  198.  
  199.         spack('D',n,(int)size,packet);         /* Send a D packet */
  200.         switch(rpack(&len,&num,recpkt))  /* What was the reply? */
  201.         {
  202.         case 'N':                      /* NAK, just stay in this state, */
  203.                 /* unless it's NAK for next packet */
  204.                 num = (--num<0 ? 63:num);
  205.                 if (n != num)      /* which is just like an ACK for */
  206.                         return(state);  /* this packet so fall thru to... */
  207.  
  208.         case 'Y':                      /* ACK */
  209.                 if (n != num) return(state); /* If wrong ACK, fail */
  210.                 numtry = 0;          /* Reset try counter */
  211.                 n = (n+1)%64;      /* Bump packet count */
  212.                 mlwrite("[Sending packet: %d]",++np);
  213.                 if ((size=bufill(packet))==EOF) /* Get data from file */
  214.                         return('Z');    /* If EOF set state to that */
  215.                 return('D');        /* Got data, stay in state D */
  216.  
  217.         case 'E':                      /* Error packet received */
  218.                 prerrpkt(recpkt);       /* Print it out and */
  219.                 return('A');        /* abort */
  220.  
  221.         case FALSE:
  222.                 return(state);    /* Receive failure, stay in D */
  223.  
  224.         default:
  225.                 return('A');    /* Anything else, "abort" */
  226.         }
  227. }
  228.  
  229.  
  230. /*
  231. *      s e o f
  232. *
  233. * Send End-Of-File.
  234. */
  235.  
  236. char seof()
  237. {
  238.         int num, len;              /* Packet number, length */
  239.         if (numtry++ > MAXTRY) return('A'); /* If too many tries, "abort" */
  240.  
  241.         spack('Z',n,0,packet);    /* Send a 'Z' packet */
  242.         switch(rpack(&len,&num,recpkt))  /* What was the reply? */
  243.         {
  244.         case 'N':                      /* NAK, just stay in this state, */
  245.                 /* unless it's NAK for next packet, */
  246.                 num = (--num<0 ? 63:num);
  247.                 if (n != num)      /* which is just like an ACK for */
  248.                         return(state);  /* this pac